package com.customization.assets.wfAction;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.CodeBuild;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @author	Huangwm
 * @description	事业单位设备登记批量入库领用状态记录资产
 */
public class CreateModeDataListSYDW implements Action{
    private LogTool log = new LogTool("/log/assetlog/huangwm/CreateModeDataListSYDW",false);

    public String execute(RequestInfo requestInfo) {
        int requestid = requestInfo.getRequestManager().getRequestid();
        int userid = requestInfo.getRequestManager().getUserId();
        log.writeLog("事业单位登记流程根据明细表数量批量生成在库实物资产数据："+requestid);
        //流程主表属性
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        Map<String,String> MainTableInfo = new HashMap<String,String>();
        for(int i=0; i<properties.length; i++){
            String name = properties[i].getName(); //字段名称
            String value = Util.null2String(properties[i].getValue()); //主字段对应的值
            MainTableInfo.put(name, value);
        }
        String tablename = requestInfo.getRequestManager().getBillTableName();
        //明细表数据直接从数据库查,方便找到明细表id
        List<Map<String, String>> detailList = new ArrayList<Map<String,String>>();
        try {
            RecordSet rs = new RecordSet();
            String getDetialSql = "select c.*,m.xgfj from "+tablename+"_dt1 c left join "+tablename+" m on c.mainid=m.id where m.requestId="+requestid;
            log.writeLog(getDetialSql);
            rs.execute(getDetialSql);
            log.writeLog(rs.getColCounts());
            while(rs.next()){
                Map<String, String> data = new HashMap<String, String>();
                data.put("id", rs.getString("id"));
                data.put("mainid", rs.getString("mainid"));
                data.put("requestid", rs.getString("requestid"));
                data.put("zcmc", rs.getString("zcmc"));//资产名称
                data.put("zclx", rs.getString("zclx"));//资产类型
                data.put("rksl", rs.getString("rksl"));//入库数量
                data.put("szck	", rs.getString("szck"));//所在仓库
                data.put("pp", rs.getString("pp"));//品牌
                data.put("ggxh", rs.getString("ggxh"));//规格型号
                data.put("dj", rs.getString("dj"));//单价
                data.put("synx", rs.getString("synx"));//使用年限
                data.put("macdz", rs.getString("macdz"));//mac地址
                data.put("jqm", rs.getString("jqm"));//机器名
                data.put("wllx", rs.getString("wllx"));//网络类型
                data.put("zcly", rs.getString("zcly"));//资产来源
                data.put("cqzk", rs.getString("cqzk"));//产权状况
                data.put("sbzt", rs.getString("sbzt"));//设备状态
                data.put("zrr", rs.getString("zrr"));//责任人
                data.put("syks", rs.getString("syks"));//使用科室
                data.put("qxbm", rs.getString("qxbm"));//权限部门
                data.put("rkrq", rs.getString("rkrq"));//入库日期
                data.put("xgfj", rs.getString("xgfj"));//相关附件
                data.put("hth", rs.getString("hth"));//合同号
                data.put("qdrq", rs.getString("qdrq"));//取得日期
                data.put("bz", rs.getString("bz"));//备注
                detailList.add(data);
            }
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("select data error");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }

        final int FORMMODEID = 102;		//模块ID
        final int MODEDATACREATERTYPE = 0;	//创建人类型
        final int BILLID = -198;	//表单id
        ModeRightInfo mri = new ModeRightInfo();
        RecordSetTrans rst1 = null;
        RecordSetTrans rst2 = null;
        RecordSet rs2 = null;
        int sum = 0;
        for (Map<String, String> map : detailList) {
            int rksl = Integer.parseInt(map.get("rksl"));
            String id = map.get("id");
            String zcmc = map.get("zcmc");
            String zclx = map.get("zclx");
            String szck = map.get("szck");
            String pp = map.get("pp");
            String ggxh = map.get("ggxh");
            String dj = map.get("dj");
            String synx = map.get("synx");
            String macdz = map.get("macdz");
            String jqm = map.get("jqm");
            String wllx = map.get("wllx");//网络类型默认为空
            String cqzk = map.get("cqzk");
            String sbzt = map.get("sbzt");
            String zrr = map.get("zrr");
            String syks = map.get("syks");
            String qxbm = map.get("qxbm");
            String rkrq = map.get("rkrq");
            String xgfj = map.get("xgfj");
            String zcly = map.get("zcly");
            String hth = map.get("hth");
            String qdrq = map.get("qdrq");
            String bz = map.get("bz");
            for(int i=1;i<=rksl;i++){
                sum++;
                log.writeLog("当前需插入"+rksl+"条，第"+(sum)+"次循环");
                String uuid = getUUID();
                rst1 = new RecordSetTrans();
                rst2 = new RecordSetTrans();
                rs2 = new RecordSet();
                try {
                    //产权状况默认为事业单位资产 cqzk=3
                    String insertMode = "insert into uf_swzczmxx(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID,reqid,mxid,zclx,cqzk,sjzt,bjwjcsb) values(";
                    insertMode += FORMMODEID+","+userid+","+MODEDATACREATERTYPE+",'"+getCurrentDate()+"','"+getCurrentTime()+"','"+uuid+"',"+requestid+","+id+","+zclx+",3,0,0)";
                    log.writeLog(insertMode);
                    boolean flag = rst1.execute(insertMode);
                    if(flag){
                        try {
                            String getBillid = "select id from uf_swzczmxx where MODEUUID='"+uuid+"'";
                            rs2.execute(getBillid);
                            if(rs2.next()){
                                //生成模块编码 参数为ModeCode表中的编码记录id
                                CodeBuild codeBuild = new CodeBuild(2);
                                String code = codeBuild.getModeCodeStr(BILLID, rs2.getInt("id"));
                                log.writeLog("billid["+rs2.getInt("id")+"]生成的建模编码为"+code);
                                //zczt 默认为1
                                Object[] object = {
                                        code,zcmc,szck,dj.equals("")?"0.0":dj,pp,pp,ggxh,ggxh,synx,macdz,jqm,cqzk,0,zrr,syks,qxbm,rkrq,xgfj,zcly,hth,qdrq,rs2.getInt("id")
                                };
                                String updateMode = "update uf_swzczmxx set swzcbh=?, zcmc=?,szck=?,dj=?,pp=?,ppss=?,ggxh=?,ggxhss=?,synx=?,macdz=?,jqm=?,cqzk=?,zczt=?,zrr=?,syks=?,dybm=?,rkrq=?,xgfj=?,zcly=?,hth=?,qdrq=?  where id=?";
                                rst2.executeUpdate(updateMode,object);
                                mri.editModeDataShare(userid, FORMMODEID, rs2.getInt("id"));
                            }
                        } catch (Exception e) {
                            rs2.execute("delete from uf_swzczmxx where reqid="+requestid);
                            e.printStackTrace();
                            requestInfo.getRequestManager().setMessageid("getbillid update error");
                            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                            return FAILURE_AND_CONTINUE;
                        }
                    }
                } catch (Exception e) {
                    rst1.rollback();
                    rs2.execute("delete from uf_swzczmxx where reqid="+requestid);
                    e.printStackTrace();
                    requestInfo.getRequestManager().setMessageid("insert error");
                    requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                    return FAILURE_AND_CONTINUE;
                }

            }
        }

        return SUCCESS;
    }

    //生成UUID
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().toLowerCase();
        return uuid;
    }

    /**
     * 获取当前日期
     * @return
     */
    public String getCurrentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 获取当前时间
     * @return
     */
    public String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }
}
